package cn.sdormitory.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.sdormitory.academic.dao.ClsPracticeInfoDao;
import cn.sdormitory.academic.entity.ClsPracticeInfo;
import cn.sdormitory.basedata.dao.BStudentDao;
import cn.sdormitory.basedata.entity.BStudent;
import cn.sdormitory.controller.vos.ParentQueryStudentInfoVo;
import cn.sdormitory.exam.dao.ExamResultInfoDao;
import cn.sdormitory.exam.vo.ExamResultInfoAppVo;
import cn.sdormitory.service.AppParentService;
import cn.sdormitory.smartclass.dao.ClassDvDao;
import cn.sdormitory.smartclass.vo.ClassDvAppVo;
import cn.sdormitory.smartdor.dao.SdAttenceDao;
import cn.sdormitory.smartdor.dao.StudentTrajectoryDao;
import cn.sdormitory.smartdor.entity.StudentTrajectory;
import cn.sdormitory.smartdor.vo.SdAttenceAppVo;
import cn.sdormitory.violative.dao.ViolationPatrolDao;
import cn.sdormitory.violative.entity.ParentAppViolationPatrol;
import cn.sdormitory.violative.entity.ViolationPatrol;
import cn.sdormitory.workflow.dao.ViolationApplyDao;
import cn.sdormitory.workflow.entity.ViolationApply;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author : yy
 * @date: 2024/11/2 15:28
 * @description:
 */
@Service
public class AppParentServiceImpl implements AppParentService {
    @Resource
    BStudentDao bStudentDao;
    @Resource
    StudentTrajectoryDao studentTrajectoryDao;
    @Resource
    ExamResultInfoDao examResultInfoDao;
    @Resource
    ClassDvDao classDvDao;
    @Resource
    SdAttenceDao sdAttenceDao;
    @Resource
    private ViolationPatrolDao violationPatrolDao;
    
    @Resource
    private ViolationApplyDao violationApplyDao;
    
    @Resource
    private ClsPracticeInfoDao clsPracticeInfoDao;


    @Override
    public IPage<StudentTrajectory> getParenChildTrajectory(ParentQueryStudentInfoVo queryVo) {
        IPage<StudentTrajectory> page = new Page<>(queryVo.getPageNum(), queryVo.getPageSize());
        
        return studentTrajectoryDao.getInfoPageByStudentId(queryVo.getStudentId(), page);
    }

    @Override
    public List<ExamResultInfoAppVo> getParenChildScore(Long studentId) {
        BStudent student = bStudentDao.selectById(studentId);
        return examResultInfoDao.findResultByStudentNo(student.getStudentNo());
    }

    @Override
    public List<ParentAppViolationPatrol> getParenChildDv(Long studentId) {
        List<ParentAppViolationPatrol> resultList = new ArrayList<>();
        
        LambdaQueryWrapper<ViolationPatrol> patrolLambdaQueryWrapper = new LambdaQueryWrapper<>();
        patrolLambdaQueryWrapper.eq(ViolationPatrol::getStudentId, studentId);
        List<ViolationPatrol> violationPatrols = violationPatrolDao.selectList(patrolLambdaQueryWrapper);
        if (CollectionUtil.isEmpty(violationPatrols)) {
            return resultList;
        }
    
        for (ViolationPatrol violationPatrol : violationPatrols) {
            ParentAppViolationPatrol patrol = new ParentAppViolationPatrol();
            patrol.setStudentId(violationPatrol.getStudentId());
            patrol.setStudentName(violationPatrol.getStudentName());
            patrol.setViolationTime(DateUtil.formatDate(violationPatrol.getViolationTime()));
            patrol.setViolationContent(violationPatrol.getNote());
            patrol.setDealContent(violationPatrol.getHandleContent());
            resultList.add(patrol);
        }
        
        return resultList;
    }

    @Override
    public IPage<SdAttenceAppVo> getParenChildDorAttendance(ParentQueryStudentInfoVo queryVo) {
        BStudent student = bStudentDao.selectById(queryVo.getStudentId());
        IPage<SdAttenceAppVo> page = new Page<>(queryVo.getPageNum(), queryVo.getPageSize());
    
        return sdAttenceDao.getAttencePageByStudentNo(student.getStudentNo(), page);
    }
    
    @Override
    public List<ClsPracticeInfo> getClsPractice(Long studentId) {
        BStudent student = bStudentDao.selectById(studentId);
        return clsPracticeInfoDao.findClsPracticeByStudentNo(student.getStudentNo());
    }
    
    @Override
    public StudentTrajectory getStudentTrajectory(String studentId, String faceTime) {
        return studentTrajectoryDao.getTrajectoryInfoByStudentId(studentId, faceTime);
    }
    
    @Override
    public SdAttenceAppVo getStudentDorAttendance(String studentId, String datetime) {
        return sdAttenceDao.getStudentDorAttendance(studentId, datetime);
    }
}
